﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Data.Json;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http;
using System.Collections.ObjectModel;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=391641 上有介绍

namespace App1
{



    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;

            //fpview.SelectionChanged += fpview_SelectionChanged;
        }

        

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。
        /// 此参数通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: 准备此处显示的页面。

            // TODO: 如果您的应用程序包含多个页面，请确保
            // 通过注册以下事件来处理硬件“后退”按钮:
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed 事件。
            // 如果使用由某些模板提供的 NavigationHelper，
            // 则系统会为您处理该事件。
        }



        //List<int> k = new List<int>();

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var list = await DataModel.GetBookList(keyword.Text);
            addItemsToFilpView(list);
            fpview.SelectionChanged += fpview_SelectionChanged;
        }


        private void addItemsToFilpView(BookSearchList list)
        {
            foreach (var flipviewitem in list.booksearchlist)
            {
                ListView a = new ListView()
                {
                    ItemTemplate = Resources["listviewtemplate"] as DataTemplate,
                    ItemsSource = flipviewitem.books,
                    Height = 400
                };
                fpview.Items.Insert(fpview.Items.Count, a);
            }
        }

        private async void fpview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int currentItemTotal = fpview.Items.Count;
            if (currentItemTotal < fpview.SelectedIndex + 2)
            {
                var list = await DataModel.GetBookList(keyword.Text, currentItemTotal, 3);
                addItemsToFilpView(list);
            }
            //throw new NotImplementedException();
        }



    }
}
